<?php

/**
 * This is the model class for table "xman_message".
 *
 * The followings are the available columns in table 'xman_message':
 * @property integer $message_id
 * @property string $message_subject
 * @property string $message_content
 * @property integer $message_category_id
 * @property integer $message_sent_by_user_id
 * @property string $message_sent_date
 *
 * The followings are the available model relations:
 * @property MessageCategory $messageCategory
 * @property User $messageSentByUser
 * @property MessageUser[] $messageUsers
 */
class Message extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Message the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_message';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('message_subject, message_content', 'required'),
			array('message_category_id, message_sent_by_user_id', 'numerical', 'integerOnly'=>true),
			array('message_subject', 'length', 'max'=>50),
			array('message_content', 'length', 'max'=>255),
			array('message_sent_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('message_id, message_subject, message_content, message_category_id, message_sent_by_user_id, message_sent_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'messageCategory' => array(self::BELONGS_TO, 'MessageCategory', 'message_category_id'),
			'messageSentByUser' => array(self::BELONGS_TO, 'User', 'message_sent_by_user_id'),
			'messageUsers' => array(self::HAS_MANY, 'MessageUser', 'message_user_message_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'message_id' => 'Message',
			'message_subject' => 'Message Subject',
			'message_content' => 'Message Content',
			'message_category_id' => 'Message Category',
			'message_sent_by_user_id' => 'Message Sent By User',
			'message_sent_date' => 'Message Sent Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('message_id',$this->message_id);
		$criteria->compare('message_subject',$this->message_subject,true);
		$criteria->compare('message_content',$this->message_content,true);
		$criteria->compare('message_category_id',$this->message_category_id);
		$criteria->compare('message_sent_by_user_id',$this->message_sent_by_user_id);
		$criteria->compare('message_sent_date',$this->message_sent_date,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        /**
         *  Return all comment associative with message
         */
        public function messageComments() {
                $result = Comment::model()->findAllByAttributes(array('comment_object_type'=>'MESSAGE','comment_object_id'=>$this->message_id));
                return $result;
        }
        
        /**
         * Get list of message category asscocative with project
         */
        public function getListCategory($pid) {
        	$messageCategories = MessageCategory::model()->findAllByAttributes(array('message_category_project_id'=>$pid),array('order'=>'message_category_name ASC'));
        	
        	$list = array();
        	foreach ($messageCategories as $messageCategory) {
        		$list[$messageCategory->message_category_id] = $messageCategory->message_category_name;
        	}
        	$list["-"] = " - New Category -";
        	return $list;
        }
        
        /**
         * Get list of message people asscocative with project
         */
        public function getListUser($pid) {
        	$projectUsers = ProjectUser::model()->findAllByAttributes(array("project_user_project_id"=>$pid));
        	
        	$list = array();
        	foreach ($projectUsers as $projectUser) {
        		$list[$projectUser->project_user_user_id] = $projectUser->projectUserUser->user_name;
        	}
        	return $list;
        }
}